package club.mzywucai.blog.file_system.util;

import java.text.DecimalFormat;

/**
 * @author mzywucai
 * @Description
 * @date 2019/1/30
 */
public class SizeToStringUtil {
    public static String getSizeString(Long size) {
        DecimalFormat df = new DecimalFormat("#.00");

        String sizeString;
        double tempSize;
        if(size >= 1024 && size < 1024 * 1024) {
            tempSize = size/1024.0;
            sizeString = df.format(tempSize)+"KB";
        } else if(size > 1024 * 1024 &&
                size <= 1024 * 1024 * 1024) {
            tempSize = size/(1024*1024.0);
            sizeString = df.format(tempSize)+"MB";
        } else if(size >= 1024 * 1024 * 1024) {
            tempSize = size/(1024*1024*1024.0);
            sizeString = df.format(tempSize)+"GB";
        } else {
            sizeString = size+"B";
        }

        return sizeString;
    }
}
